<template>
	<div style="width: 100%;height: 100%">
		<el-row style="height: 10%;">
			<el-form class="search-form form-inline" label-width="120px" size="mini" ref="searchForm">
				<el-col :span="8">
					<el-form-item class="form-group" label="航线号：">
						<el-input v-model="searchForm.routeNo" placeholder="" clearable />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item class="form-group" label="始发城市：">
						<el-input v-model="searchForm.startCity" placeholder="" clearable />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item class="form-group" label="终点城市：">
						<el-input v-model="searchForm.endCity" placeholder="" clearable />
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<el-row style="height: 10%;">
			<el-button type="primary" size="mini" plain class="el-icon-search" @click="doSearch(1)">查询</el-button>
			<el-button v-if="auth == 0" type="primary" size="mini" plain class="el-icon-edit" @click="doAdd()">新增
			</el-button>
			<el-button v-if="auth == 0" type="primary" size="mini" plain class="el-icon-edit" @click="doUpdate()">修改
			</el-button>
			<el-button v-if="auth == 0" type="primary" size="mini" plain class="el-icon-circle-close"
				@click="doDelate()">删除</el-button>
		</el-row>
		<el-row style="height: 75%;">
			<el-table ref="table" :data="tableData" style="position: absolute" height="100%" size="mini"
				highlight-current-row border @current-change="handleCurrentRow"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="60" align="center" />
				<el-table-column prop="routeNo" align="center" label="航线号" :min-width="150" />
				<el-table-column prop="startCity" align="center" label="始发城市" width="200" />
				<el-table-column prop="endCity" align="center" label="终点城市" width="200" />
			</el-table>
		</el-row>
		<el-row style="height: 5%;">
			<el-pagination :page-sizes="[30, 50, 100]" :current-page="searchForm.pageNo"
				:page-size="searchForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"
				@size-change="handleSizeChange" @current-change="handleCurrentChange" />
		</el-row>

		<el-dialog :title="title" :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
			<el-form class="search-form form-inline" label-width="120px" size="mini" ref="routeFrom">
				<el-row>
					<el-col :span="24">
						<el-form-item class="form-group" label="航线号：">
							<el-input v-model="routeFrom.routeNo" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="始发城市">
							<el-input v-model="routeFrom.startCity" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="终点城市：">
							<el-input v-model="routeFrom.endCity" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import axios from 'axios'
	export default {
		props: {
			auth: {
				type: String
			}
		},
		data() {
			return {
				searchForm: {
					startCity: '',
					endCity: '',
					routeNo: '',
					pageNo: 1,
					pageSize: 30
				},
				tableData: [],
				totalCount: 0,
				title: '',
				dialogVisible: false,
				selection: [],
				routeFrom: {
					id: '',
					routeNo: '',
					startCity: '',
					endCity: ''
				}
			}
		},
		methods: {
			doSearch(pageNo) {
				this.searchForm.pageNo = pageNo
				this.$axios.post("http://localhost:8888/route/getPage?pageNum=" + this.searchForm.pageNo +
						"&pageSize=" + this.searchForm.pageSize, this.searchForm)
					.then((data) => {
						console.log(data)
						this.tableData = data.data.records
						this.totalCount = data.data.total
					})
			},
			handleCurrentChange(val) {
				console.log(val)
				this.doSearch(val)
			},
			handleSizeChange(val) {
				console.log(val)
				this.searchForm.pageSize = val
				this.doSearch(1)
			},
			handleSelectionChange(selection) {
				this.selection = selection
			},
			doAdd() {
				this.title = '新增航线信息'
				this.dialogVisible = true
			},
			doUpdate() {
				this.title = '修改航线信息'
				if (this.selection.length == 1) {
					this.routeFrom = this.selection[0]
					this.dialogVisible = true
				} else if (this.selection.length == 0) {
					this.$message.warning('请选择一条信息进行修改');
				} else {
					this.$message.warning('只能选择一条信息进行修改');
				}
			},
			save() {
				console.log(this.routeFrom.id)
				if (this.routeFrom.id !== '') {
					this.$axios.post("http://localhost:8888/route/update", this.routeFrom).then((data) => {
						if (data.data == 1) {
							this.dialogVisible = false
							this.routeFrom = {
								id: '',
								routeNo: '',
								startCity: '',
								endCity: ''
							}
							this.$message.success('修改成功');
							this.doSearch(1)
						}
					})
				} else {
					this.$axios.post("http://localhost:8888/route/insert", this.routeFrom).then((data) => {
						if (data.data == 1) {
							this.dialogVisible = false
							this.routeFrom = {
								id: '',
								routeNo: '',
								startCity: '',
								endCity: ''
							}
							this.$message.success('新增成功');
							this.doSearch(1)
						}
					})
				}
			},
			doDelate() {
				if (this.selection.length == 0) {
					this.$message.warning('请选择一条信息进行修改');
				} else {
					let ids = []
					this.selection.forEach((item) => {
						ids.push(item.id)
					})
					this.$axios.post("http://localhost:9999/student/info/delete?ids=" + ids.join(",")).then((data) => {
						if (data.data >= 1) {
							this.$message.success('删除成功');
							this.doSearch(1)
						}
					})
				}

			},
		},
		mounted() {
			this.doSearch(1)
		}
	}
</script>

<style scoped>
</style>
